package com.ucenter.server.org.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.system.comm.model.Page;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;
import com.ucenter.server.org.dao.PositionUserDao;
import com.ucenter.server.org.pojo.DepInfo;
import com.ucenter.server.org.pojo.PositionInfo;
import com.ucenter.server.org.pojo.PositionUser;
import com.ucenter.server.org.service.DepInfoService;
import com.ucenter.server.org.service.PositionInfoService;
import com.ucenter.server.org.service.PositionUserService;

/**
 * position_user的Service
 * @author autoCode
 * @date 2018-12-01 09:50:14
 * @version V1.0.0
 */
@Component
public class PositionUserServiceImpl implements PositionUserService {

	@Autowired
	private PositionUserDao positionUserDao;
	@Autowired
	private PositionInfoService positionInfoService;
	@Autowired
	private DepInfoService depInfoService;
	
	@Override
	public ResponseFrame save(PositionUser positionUser) {
		ResponseFrame frame = new ResponseFrame();
		PositionUser org = get(positionUser.getUserId(), positionUser.getPositionId());
		PositionInfo position = positionInfoService.get(positionUser.getPositionId());
		DepInfo dep = depInfoService.get(position.getDepId());
		positionUser.setOrgId(dep.getOrgId());
		if (org == null) {
			positionUserDao.save(positionUser);
		}
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public PositionUser get(String userId, String positionId) {
		return positionUserDao.get(userId, positionId);
	}

	@Override
	public ResponseFrame pageQuery(PositionUser positionUser) {
		positionUser.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = positionUserDao.findPositionUserCount(positionUser);
		List<PositionUser> data = null;
		if(total > 0) {
			data = positionUserDao.findPositionUser(positionUser);
		}
		Page<PositionUser> page = new Page<PositionUser>(positionUser.getPage(), positionUser.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(String userId, String positionId) {
		ResponseFrame frame = new ResponseFrame();
		positionUserDao.delete(userId, positionId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public List<PositionUser> findByUserId(String userId) {
		return positionUserDao.findByUserId(userId);
	}

	@Override
	public void deleteByUserId(String userId) {
		positionUserDao.deleteByUserId(userId);
	}
}